package sunmi.payment.L3;

import mi.payment.MyApplication;
import mi.payment.db.PayDetailModel;
import mi.payment.db.SettlementModel;
import mi.payment.db.dao.PaySettlement;
import mi.payment.settings.SignOperation;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.utils.SystemDateTime;
import mi.payment.utils.transaction.ParamsInitTask;

/**
 * L3 交易判断处理
 *
 * @author Created by Lee64 on 2018/3/2.
 */

public class L3TransactionUtil {

    // 连接PayHardwareService成功？ --> 签到成功？ --> 交易开关打开？

    public static boolean isDisconnectPayHardwareService() {
        return MyApplication.getInstance().pinPadOpt == null;
    }

    /**
     * 判断是否签到
     */
    public static boolean isNotSign() {
        // 获取最后签到的时间
        int lastSignTime = SignOperation.getInstance().getLastSignTime();
        int currentYYYYMMDD = SystemDateTime.getCurrentYYYYMMDD();
        if (lastSignTime == currentYYYYMMDD) {
            ParamsInitTask.isChecked = true;
            return false;
        }
        String batchNumStart = SystemParamsOperation.getInstance().getSystemParamsSettings().getBatchNumStart();
        PaySettlement paySettlement = SettlementModel.getInstance().getPaySettlementByBatchNo(batchNumStart);
        if (ParamsInitTask.isChecked && paySettlement != null) {
            return false;
        }
        return true;
    }

    /**
     * 判断交易笔数上限
     */
    public static boolean isTransactionNumberOver() {
        int maxCount = SystemParamsOperation.getInstance().getSystemParamsSettings().getTransactionNumber();
        int currentCount = PayDetailModel.getInstance().getPayDetailCount();
        return currentCount >= maxCount;
    }

    /**
     * 判断交易开关
     *
     * @param requestType 请求类型
     * @param payType     支付方式
     * @return 开启：true  关闭：false
     */
    public static boolean isTransactionSwitchClose(int requestType, int payType) {
        boolean isOpen = true;
        TransactionSwitchSetting setting = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting();
        switch (requestType) {
            case L3Constant.RequestType.CONSUMPTION:
                if (payType == L3Constant.PayType.BARCODE_TYPE_BANK) {
                    isOpen = setting.isConsumption();
                } else {
                    isOpen = isQRCodeTransactionSwitch(payType);
                }
                break;
            case L3Constant.RequestType.CONSUMPTION_REVOKE:
                if (payType == L3Constant.PayType.BARCODE_TYPE_BANK) {
                    isOpen = setting.isConsumptionRevoke();
                } else {
                    isOpen = isQRCodeTransactionSwitch(payType);
                }
                break;
            case L3Constant.RequestType.RETURN_GOODS:
                if (payType == L3Constant.PayType.BARCODE_TYPE_BANK) {
                    isOpen = setting.isReturnGoods();
                } else {
                    isOpen = isQRCodeTransactionSwitch(payType);
                }
                break;
            case L3Constant.RequestType.FIELD_AUTH:
                isOpen = setting.isFieldAuth();
                break;
            case L3Constant.RequestType.FIELD_AUTH_REVOKE:
                isOpen = setting.isFieldAuthRevoke();
                break;
            case L3Constant.RequestType.FIELD_AUTH_COMPLETE:
                isOpen = setting.isFieldAuthReq();
                break;
            case L3Constant.RequestType.FIELD_AUTH_COMPLETE_REVOCATION:
                isOpen = setting.isFieldAuthCompleteRevocation();
                break;
            case L3Constant.RequestType.QUERY_BALANCE:
                isOpen = setting.isQueryBalance();
                break;
        }
        if (isOpen) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 扫码类交易开关
     */
    private static boolean isQRCodeTransactionSwitch(int payType) {
        boolean state;
        TransactionSwitchSetting setting = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting();
        if (payType == L3Constant.PayType.BARCODE_TYPE_AliPay_QR) {
            state = setting.isUseAliPayQR();
        } else if (payType == L3Constant.PayType.BARCODE_TYPE_AliPay_SCAN) {
            state = setting.isUseAliPayScan();
        } else if (payType == L3Constant.PayType.BARCODE_TYPE_WeChat_QR) {
            state = setting.isUseWeChatQR();
        } else if (payType == L3Constant.PayType.BARCODE_TYPE_WeChat_SCAN) {
            state = setting.isUseWeChatScan();
        } else if (payType == L3Constant.PayType.BARCODE_TYPE_UNION_QR) {
            state = setting.isUseUnionPayQR();
        } else if (payType == L3Constant.PayType.BARCODE_TYPE_UNION_SCAN) {
            state = setting.isUseUnionPayScan();
        } else {
            state = true;
        }
        return state;
    }


}
